package com.app.traval.service;

import com.app.traval.domain.Opinion;
import com.app.traval.repository.OpinionRepository;
import com.app.traval.repository.search.OpinionSearchRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Opinion.
 */
@Service
@Transactional
public class OpinionService {

    private final Logger log = LoggerFactory.getLogger(OpinionService.class);
    
    @Inject
    private OpinionRepository opinionRepository;
    
    @Inject
    private OpinionSearchRepository opinionSearchRepository;
    
    /**
     * Save a opinion.
     * 
     * @param opinion the entity to save
     * @return the persisted entity
     */
    public Opinion save(Opinion opinion) {
        log.debug("Request to save Opinion : {}", opinion);
        Opinion result = opinionRepository.save(opinion);
        opinionSearchRepository.save(result);
        return result;
    }

    /**
     *  Get all the opinions.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public Page<Opinion> findAll(Pageable pageable) {
        log.debug("Request to get all Opinions");
        Page<Opinion> result = opinionRepository.findAll(pageable); 
        return result;
    }

    /**
     *  Get one opinion by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public Opinion findOne(Long id) {
        log.debug("Request to get Opinion : {}", id);
        Opinion opinion = opinionRepository.findOne(id);
        return opinion;
    }

    /**
     *  Delete the  opinion by id.
     *  
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Opinion : {}", id);
        opinionRepository.delete(id);
        opinionSearchRepository.delete(id);
    }

    /**
     * Search for the opinion corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<Opinion> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Opinions for query {}", query);
        return opinionSearchRepository.search(queryStringQuery(query), pageable);
    }
}
